package com.baizhi.service.impl;

import com.baizhi.dao.CategoryMapper;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryExample;
import com.baizhi.service.CategoryService;
import com.baizhi.vo.CategoryVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {


    @Resource
    private CategoryMapper categoryMapper;

    //分页查询一级类别数据
    @Override
    public CategoryVO queryOnePage(Integer page, Integer pageSize) {
        CategoryVO categoryVO = new CategoryVO();
        CategoryExample example = new CategoryExample();
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        example.createCriteria().andLevelNotEqualTo(2);  	//添加xxx字段不等于value条件
        example.setOrderByClause("id asc");  //asc  desc
        //查询
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        categoryVO.setPage(page);
        int total = categoryMapper.selectCountByExample(example);
        categoryVO.setTotal(total);
        categoryVO.setRows(categoryList);
        return categoryVO;
    }

    //添加一级类别
    @Override
    public Map<String, String> add(Category category) {
        Map<String, String> map = new HashMap<>();
        category.setLevel(1);
        int i = categoryMapper.insert(category);
        if (i == 1){
            map.put("message","添加成功");
            map.put("status","200");
        }else {
            map.put("message","添加失败");
            map.put("status","400");
        }
        return map;
    }

    //删除级别
    @Override
    public Map<String, String> delete(Integer id,Integer parentId) {
        Map<String, String> map = new HashMap<>();
        /*int i = categoryMapper.deleteByPrimaryKey(category.getId());*/
        if (parentId == null){
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(id);
            int twoCount = categoryMapper.selectCountByExample(categoryExample);
            if (twoCount == 0){
                categoryMapper.deleteByPrimaryKey(id);
                map.put("message","删除一级类别成功");
            }else {
                map.put("message","删除失败,一级类别下有二级类别");
            }
        }else {
            categoryMapper.deleteByPrimaryKey(id);
            map.put("message","删除二级类别成功");
        }
        return map;
    }

    //修改一级类别的数据
    @Override
    public Map<String, String> update(Category category) {
        Map<String, String> map = new HashMap<>();
        int i = categoryMapper.updateByPrimaryKeySelective(category);
        if (i == 1){
            map.put("message","修改成功");
            map.put("status","200");
        }else {
            map.put("message","修改失败");
            map.put("status","400");
        }
        return map;
    }

    //根据一级类别Id查询二级类别数据
    @Override
    public CategoryVO queryTwoPage(Integer page ,Integer pageSize,Integer categoryId) {
        CategoryVO categoryVO = new CategoryVO();
        CategoryExample example = new CategoryExample();
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        example.createCriteria().andParentIdEqualTo(categoryId);
        example.setOrderByClause("id desc");
        //查询
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        categoryVO.setPage(page);
        int total = categoryMapper.selectCountByExample(example);
        categoryVO.setTotal(total);
        categoryVO.setRows(categoryList);
        return categoryVO;
    }

    //根据级别查询类别
    @Override
    public List<Category> queryByLevelsCategory(Integer levels) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo(levels);
        return categoryMapper.selectByExample(categoryExample);
    }

    @Override
    public Category queryById(int id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

}
